import pinia from "@/stores/store"
import { useUserStore } from '@/stores/user'

const userStore = useUserStore(pinia)


function checkPermission(el, binding) { 
  const { value } = binding 
  const permissions = userStore.perms
  const all_permission = '*'
  if (Array.isArray(value)) {
      if (value.length > 0) {
          const hasPermission = permissions.some((key) => {
              return all_permission == key || value.includes(key)
          })

          if (!hasPermission) {
              el.parentNode && el.parentNode.removeChild(el)
          }
      }
  } else {
      throw new Error('like v-perms="[\'auth.menu/edit\']"')
  }
}

export default {
  // 在绑定元素的父组件被挂载后调用
  mounted(el, binding) {
    checkPermission(el, binding)
  },
  // 在包含组件的 VNode 及其子组件的 VNode 更新后调用
  update(el, binding) {
    checkPermission(el, binding)
  }
}
